package com.weitu.occupationalHealth.station.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.occupationalHealth.station.service.StationService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * Created by WT-SUN on 2017/4/14.
 */
@Controller
@RequestMapping(value = "occupationalHealth/station")
public class StationController extends BaseController{
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "occupationalHealth/station/";

    @Autowired
    private StationService stationService;

    /**
     * 跳转至岗位值页面
     *
     * @return
     */
    @RequestMapping(value = "/list")
    public String list() {
        return MODULE_BATH_PATH + "list";
    }

    /**
     * 岗位值超标整改页面
     * @return
     */
    @RequestMapping(value = "/c/list")
    public String clist() {
        return MODULE_BATH_PATH + "cList";
    }

    /**
     * 新增页面
     *
     * @return
     */
    @RequestMapping(value = "/add")
    public String add() {
        return MODULE_BATH_PATH + "add";
    }

    /**
     * 跳转详情与编辑页面
     *
     * @param id
     * @return
     */
    @RequestMapping("info")
    public ModelAndView info(String id, String type) {
        ModelAndView modelAndView = new ModelAndView();
        Map map=stationService.selectById(id);
        modelAndView.addObject("sta", map);
        if (type.equals("info")) {
            modelAndView.setViewName(MODULE_BATH_PATH + "info");
        } else {
            modelAndView.setViewName(MODULE_BATH_PATH + "edit");
        }
        return modelAndView;
    }

    /**
     * 添加岗位值
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("insertSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——岗位值", methods = "添加岗位值")
    public AjaxResult insertSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(paramMap);
        paramMap.put("delState",0);
        int boo = stationService.insertSelective(paramMap);
        if (boo > 0) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }

    /**
     * 更新—岗位值
     * @return
     */
    @ResponseBody
    @RequestMapping("updateByIdSelective")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——岗位值", methods = "更新岗位值")
    public AjaxResult updateByIdSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);
        int boo=stationService.updateByIdSelective(paramMap);
        if (boo > 0) {
            return success("更新成功");
        } else {
            return fail("更新失败");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    @Transactional(readOnly = false)
    @SystemLog(module = "职业卫生——岗位值", methods = "删除岗位值")
    public AjaxResult deleteById(String id){
        int boo=stationService.deleteById(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    /**
     * 岗位值列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/stationTable", method = RequestMethod.POST)
    public Object stationTable(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);
        int totalNum=stationService.selectStationCount(paramMap);
        DataGridPagination dataGridPagination= DataGridPagination.getInstance(request,totalNum);
        PageView pageView=dataGridPagination.getPageView();
        if (paramMap.get("sort") == null) {
            pageView.setSortColumn("addTime");
        }
        paramMap.put("pageView", pageView);
        List<Map> maps=stationService.selectAllStation(paramMap);
        Object data=dataGridPagination.flushDataGridData(maps);
        return data;
    }
}
